package com.bankcomm.ui.specialwidget;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.AdapterView.OnItemClickListener;
import com.bankcomm.R;
import com.bankcomm.common.ConfigManager;
import com.bankcomm.common.LPMid;
import com.bankcomm.ui.BaseViewCopy;
import com.bankcomm.ui.EMPView;
import com.bankcomm.ui.specialwidget.LPGallery.MyGallery.GalleryAdapter;
import com.rytong.tools.ui.Component;
import com.rytong.tools.utils.AsyncImageLoader;
import com.rytong.tools.utils.Utils;
import com.rytong.tools.utils.AsyncImageLoader.ImageCallBack;

/**
 * 画廊控件
 * 
 * @author
 * 
 * @date
 */
public class LPGallery extends Component {
	/** 所有的图片地址 */
	private ArrayList<String> imageAddres;

	private ArrayList<String> jumpUrlList;

	/** 当前的活动 */
	private EMPView bv;

	/** 适配器 */
	private GalleryAdapter galleryAdapter = null;

	private ArrayList<ImageView> imageViews = null;

	@Override
	public void initRealView(Activity activity, String nodeValue) {
		realView_ = new MyGallery(activity);
	}

	class MyGallery extends FrameLayout implements CompositedComponent {

		/** 名称 */
		public String attrName;

		/** 顶部的Gallery */
		private Gallery innerGallery;

		/** 底部的圆圈 */
		private RadioGroup arcs;

		private LinearLayout bottomLayout;

		private Paint paint;

		private int seatStatusHeight;

		private RectF Rect;

		private String needRound;

		private int width;

		public MyGallery(final Context context) {
			super(context);

			bv = (EMPView) context;

			imageAddres = new ArrayList<String>();
			jumpUrlList = new ArrayList<String>();

			needRound = getPropertyByName("round");

			innerGallery = new Gallery(context);
			arcs = new RadioGroup(context);
			arcs.setOrientation(RadioGroup.HORIZONTAL);

			Object srcs = getPropertyByName("src");
			if (!"".equals(srcs)) {
				reDrawArcs((String) srcs);
			}

			Object links = getPropertyByName("link");
			if (!"".equals(links)) {
				saveUrl((String) links);
			}

			imageViews = new ArrayList<ImageView>();
			for (int i = 0; i < imageAddres.size(); i++) {
				ImageView iv = new ImageView(bv);
				iv.setScaleType(ImageView.ScaleType.FIT_XY);
				iv.setLayoutParams(new Gallery.LayoutParams(
						android.view.ViewGroup.LayoutParams.FILL_PARENT, -1));
				imageViews.add(iv);
			}

			this.setBackgroundColor(Color.TRANSPARENT);
			galleryAdapter = new GalleryAdapter(context);

			innerGallery.setAdapter(galleryAdapter);
			innerGallery.setHorizontalFadingEdgeEnabled(false);
			innerGallery.setSelection(0);
			innerGallery.setSpacing(0);
			innerGallery.setFadingEdgeLength(0);
			innerGallery.setUnselectedAlpha(1.0f);
			innerGallery
					.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
						@Override
						public void onItemSelected(AdapterView<?> arg0,
								View arg1, int arg2, long arg3) {
							arcs.check(arg2 % imageAddres.size());
						}

						@Override
						public void onNothingSelected(AdapterView<?> arg0) {

						}

					});

			innerGallery.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					try {
						String link = jumpUrlList.get(position);
						if (link.startsWith("local:")) {
							link = ConfigManager.RUNAPP_URI.concat(link
									.replace("local:", ""));
						} else if (link.startsWith("web:")) {
							link = link.replace("web:", "inner:");
						}

						LPMid.getInstance().um_.dealWithLink(link,
								(BaseViewCopy) getContext());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});

			// 添加到容器
			addView(innerGallery, new FrameLayout.LayoutParams(-1, -1));

			// 添加到容器
			LinearLayout.LayoutParams arcLayout = new LinearLayout.LayoutParams(
					-2, -2);
			arcLayout.gravity = Gravity.CENTER;
			arcLayout.rightMargin = 50;

			FrameLayout.LayoutParams btmLayout = new FrameLayout.LayoutParams(
					-1, 20);
			btmLayout.gravity = Gravity.BOTTOM;

			bottomLayout = new LinearLayout(context);
			bottomLayout.setBackgroundColor(android.graphics.Color.argb(150, 0,
					0, 0));
			bottomLayout.addView(arcs, arcLayout);
			bottomLayout.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
			addView(bottomLayout, btmLayout);
			this.setLayoutParams(new LayoutParams(-1, -1));
		}

		/**
		 * 构造所有的底部小圆
		 * 
		 * @param imgUrls
		 *            图片地址列表, 以分号分割.
		 */
		public void reDrawArcs(String imgUrls) {
			if (imgUrls == null) {
				return;
			}
			if (arcs == null) {
				arcs = new RadioGroup(getContext());
			}
			arcs.removeAllViews();
			imageAddres.clear();

			String[] urls = imgUrls.split("\\|");

			for (String s : urls) {
				if (!"".equals(s)) {
					imageAddres.add(s);
				}
			}

			RadioGroup.LayoutParams radioLayout = new RadioGroup.LayoutParams(
					10, 10);
			for (int i = 0; i < urls.length; i++) {
				RadioButton rb = new RadioButton(bv);
				rb.setId(i);
				rb.setButtonDrawable(android.R.color.transparent);
				rb.setClickable(false);
				rb.setBackgroundResource(R.drawable.adv_gallery_mark_selector);
				this.arcs.addView(rb, radioLayout);
			}
		}

		public void saveUrl(String links) {
			if (links == null) {
				return;
			}
			String[] linkArr = links.split("\\|");
			for (String s : linkArr) {
				if (!"".equals(s)) {
					jumpUrlList.add(s);
				}
			}
		}

		class GalleryAdapter extends BaseAdapter {
			private Context context = null;
			public AsyncImageLoader asyncImageLoader = null;

			public GalleryAdapter(Context context) {
				this.context = context;
				asyncImageLoader = Utils.getAsyncImageLoader();
			}

			@Override
			public int getCount() {
				return imageViews != null ? imageViews.size() : 0;
				// return Integer.MAX_VALUE;
			}

			@Override
			public Object getItem(int position) {
				return imageViews != null ? imageViews.get(position) : null;
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {

				ImageView imageView = imageViews.get(position);
				String curr_URL = imageAddres
						.get(position % imageAddres.size());

				// 绑定图片
				// if (needRound.equals("true")) {
				// asyncImageLoader.downloadBitmap(context,curr_URL,callBack);
				// Bitmap bitmap = null;
				// bitmap = asyncImageLoader.getBitmapFromCache(curr_URL);
				// if(null != bitmap){
				// imageView.setImageBitmap(bitmap);
				// }else{
				// imageView.setImageResource(R.drawable.img_waiting);
				// }
				// }else{
				// asyncImageLoader.downloadBitmap(context,curr_URL,callBack);
				// Bitmap bitmap = null;
				// bitmap = asyncImageLoader.getBitmapFromCache(curr_URL);
				// if(null != bitmap){
				// imageView.setImageBitmap(bitmap);
				// }else{
				// imageView.setImageResource(R.drawable.img_waiting);
				// }
				// }
				asyncImageLoader.downloadBitmap(context, curr_URL, callBack);
				Bitmap bitmap = null;
				bitmap = asyncImageLoader.getBitmapFromCache(curr_URL);

				if (needRound.equals("true")) {
					if (null != bitmap) {
						bitmap = Utils.toRoundCorner(bitmap, getContext());
					} else {
						BitmapDrawable waitImage = (BitmapDrawable) getContext()
								.getResources().getDrawable(
										R.drawable.img_waiting);
						bitmap = waitImage.getBitmap();
						bitmap = Utils.toRoundCorner(bitmap, getContext());
					}
					imageView.setImageBitmap(bitmap);
				} else {
					if (null == bitmap) {
						BitmapDrawable waitImage = (BitmapDrawable) getContext()
								.getResources().getDrawable(
										R.drawable.img_waiting);
						bitmap = waitImage.getBitmap();
					}
					imageView.setImageBitmap(bitmap);
				}
				
				return imageView;
			}

		}

		public void setupBitmap(ImageView iv, Drawable drawable) {
			if (drawable != null) {
				if (needRound.equals("true")) {
					Bitmap bit = ((BitmapDrawable) drawable).getBitmap();
					Bitmap bitmap = Utils.toRoundCorner(bit, getContext());
					iv.setImageBitmap(bitmap);
				} else {
					iv.setImageDrawable(drawable);
				}
			} else {
				iv.setImageResource(R.drawable.img_waiting);
			}
		}

		@Override
		public Component composited() {
			return LPGallery.this;
		}
	}

	@Override
	public void mouldH() throws Exception {
		// TODO Auto-generated method stub
		super.mouldH();
	}

	@Override
	public void mouldW() throws Exception {
		// TODO Auto-generated method stub
		super.mouldW();
	}

	@Override
	public void releaseResource(Component component) {
		// TODO Auto-generated method stub
		super.releaseResource(component);

		try {
			if (null != imageViews) {
				int size = imageViews.size();
				if (size > 0) {
					// for(int i = 0;i < size;i++){
					// Utils.releaseObject(imageViews.get(i));
					// }
					imageViews.clear();
				}
				imageViews = null;
			}

			if (null != imageAddres) {
				imageAddres.clear();
				imageAddres = null;
			}

			if (null != jumpUrlList) {
				jumpUrlList.clear();
				jumpUrlList = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 图片下载的回调接口
	 */
	private ImageCallBack callBack = new ImageCallBack() {

		@Override
		public void loadImageSuccess(Bitmap bitmap, String url) {
			// TODO Auto-generated method stub
			galleryAdapter.notifyDataSetChanged();
		}
	};
}
